/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Artist_Management;


import java.sql.*;
import javax.swing.JOptionPane;
import Converters.DateConverter;


public class ArtistManagementForm extends javax.swing.JFrame {
    
    DateConverter convert = new DateConverter();
    String birthDate;

    /**
     * Creates new form ArtistManagementForm
     */
    //Μέθοδος για την εισαγωγή των στοιχείων του επιλεγμένου Καλλιτέχνη στα πεδία της φόρμας.
    public void InsertValuesToForm(){
        
                try{
        
            String url = "jdbc:derby://localhost:1527/Radiostation_DB";
            String username = "erg3";
            String password = "erg3";

            Connection con = DriverManager.getConnection(url, username, password);
            Statement stat = con.createStatement();
            
            String Query1 = "SELECT * FROM ARTIST JOIN MUSICGENRE ON ARTIST.MUSICGENRE = MUSICGENRE.MUSICGENRE_ID WHERE ARTISTICNAME = '"+ArtistsArchive.artisticName+"'";
            
            ResultSet rs = stat.executeQuery(Query1);
            
            while (rs.next()){
                
                FirstNameTxt1.setText(rs.getString(2));
                LastNameTxt.setText(rs.getString(3));
                ArtisticNameTxt2.setText(rs.getString(4));
                Sex.setSelectedItem(rs.getString(5));
                BirthDateTxt3.setText(rs.getString(6));
                BirthPlaceTxt4.setText(rs.getString(7));
                MusicGenre.setSelectedItem(rs.getString("NAME"));
                
            }
        }
        
        catch(SQLException ex){
            JOptionPane.showMessageDialog(null, ex.toString());
            }
        
    }
    
    public ArtistManagementForm() {
        initComponents();
        setLocationRelativeTo(null);//για την εμφάνιση του παραθύρου στο κέντρο
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        LastNameTxt = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        FirstNameTxt1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        ArtisticNameTxt2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        Sex = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        BirthDateTxt3 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        BirthPlaceTxt4 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        MusicGenre = new javax.swing.JComboBox();
        SaveBtn = new javax.swing.JButton();
        CancelBtn = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Φόρμα Διαχείρισης Καλλιτέχνη");
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Επώνυμο:");

        LastNameTxt.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Όνομα:");

        FirstNameTxt1.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Καλλιτεχνικό Όνομα:");

        ArtisticNameTxt2.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("Φύλλο:");

        Sex.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ΑΝΔΡΑΣ", "ΓΥΝΑΙΚΑ" }));

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("Ημερ. Γέννησης:");

        BirthDateTxt3.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("Τόπος Γέννησης:");

        BirthPlaceTxt4.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel8.setText("Είδος Μουσικής:");

        MusicGenre.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ROCK", "POP", "ΛΑΪΚΗ", "ΕΝΤΕΧΝΗ", "ΔΗΜΟΤΙΚΗ", "ΚΛΑΣΣΙΚΗ" }));

        SaveBtn.setText("Αποθήκευση");
        SaveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveBtnActionPerformed(evt);
            }
        });

        CancelBtn.setText("Ακύρωση");
        CancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelBtnActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(255, 0, 0));
        jLabel5.setText("ΗΗ/ΜΜ/ΕΕΕΕ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(175, 175, 175)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addGap(170, 170, 170)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(LastNameTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
                                    .addComponent(FirstNameTxt1)))
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(175, 175, 175)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel8)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(98, 98, 98)
                                .addComponent(SaveBtn)))
                        .addGap(103, 103, 103)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(MusicGenre, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(ArtisticNameTxt2)
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(Sex, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(BirthPlaceTxt4, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(BirthDateTxt3, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGap(0, 6, Short.MAX_VALUE))))))
                .addGap(27, 27, 27)
                .addComponent(CancelBtn)
                .addGap(99, 99, 99))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(FirstNameTxt1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(LastNameTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(ArtisticNameTxt2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(Sex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(BirthDateTxt3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(BirthPlaceTxt4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(MusicGenre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 76, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SaveBtn)
                    .addComponent(CancelBtn))
                .addGap(31, 31, 31))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void SaveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveBtnActionPerformed
      if (!ArtisticNameTxt2.getText().trim().equalsIgnoreCase("")){/*για να μην αποθηκεύουμε κενά entries, πρέπει να έχει συμπληρώσει τουλάχιστον το καλλιτεχνικό όνομα*/
          birthDate = convert.Dateconverter(BirthDateTxt3.getText());
          if (convert.flag == true){
                
        if (ArtistsArchive.FLAG == true){
            try {
                
            

                
                
            String url = "jdbc:derby://localhost:1527/Radiostation_DB";
            String username = "erg3";
            String password = "erg3";

            Connection con = DriverManager.getConnection(url, username, password);
            Statement stat = con.createStatement();
            String Query = "INSERT INTO ARTIST (FIRSTNAME , LASTNAME , ARTISTICNAME , SEX , BIRTHDAY , BIRTHPLACE , MUSICGENRE) VALUES ('"+FirstNameTxt1.getText()+"' , '"+LastNameTxt.getText()+"' , '"+ArtisticNameTxt2.getText()+"' , '"+Sex.getSelectedItem()+"' , '"+birthDate+"' , '"+BirthPlaceTxt4.getText()+"' , (SELECT MUSICGENRE_ID FROM MUSICGENRE WHERE MUSICGENRE.NAME = '"+MusicGenre.getSelectedItem()+"'))";
            
            stat.execute(Query);

            JOptionPane.showMessageDialog(null, "Επιτυχής καταχώρηση Καλλιτέχνη");
            }
            
            catch(SQLException ex){
            JOptionPane.showMessageDialog(null, ex.toString());
            }
        
            new ArtistsArchive().setVisible(true);
            dispose();
        }
        else{
            try {
            String url = "jdbc:derby://localhost:1527/Radiostation_DB";
            String username = "erg3";
            String password = "erg3";

            Connection con = DriverManager.getConnection(url, username, password);
            Statement stat = con.createStatement();
            //Query για την ανανέωση των στοιχείων στον πίνακα "Artist" της επιλεγμένης εγγραφής Καλλιτέχνη στο JTable "ArtistsTable". 
            String Query = "UPDATE ARTIST SET FIRSTNAME = '"+FirstNameTxt1.getText()+"' , LASTNAME = '"+LastNameTxt.getText()+"' , ARTISTICNAME = '"+ArtisticNameTxt2.getText()+"' , SEX =  '"+Sex.getSelectedItem()+"' , BIRTHDAY = '"+BirthDateTxt3.getText()+"' , BIRTHPLACE = '"+BirthPlaceTxt4.getText()+"' , MUSICGENRE = (SELECT MUSICGENRE_ID FROM MUSICGENRE WHERE MUSICGENRE.NAME = '"+MusicGenre.getSelectedItem()+"') WHERE ARTISTICNAME = '"+ArtistsArchive.artisticName+"'";

            stat.execute(Query);
            //Μήνυμα για την επιτυχή ανανέωση των στοιχείων.
            JOptionPane.showMessageDialog(null, "Επιτυχής ανανέωση στοιχείων Καλλιτέχνη");
            }
            
            catch(SQLException ex){
            JOptionPane.showMessageDialog(null, ex.toString());
            }
            
            ArtistsArchive.artisticName = null;//Μηδενίζουμε την τιμή για να μην σβηστεί κατά λάθος κάποια εγγραφή του πίνακα "Artist" πατώντας το κουμπί "Διαγραφή" κατά την εισαγωγή ενός νέου Καλλιτέχνη.
            new ArtistsArchive().setVisible(true);
            dispose();
        }
        }
      }else
          JOptionPane.showMessageDialog(null, "Πρέπει να συμπληρώσετε τουλάχιστον το καλλιτεχνικό όνομα για να μπορέσετε να κάνετε αποθήκευση");
        
    }//GEN-LAST:event_SaveBtnActionPerformed

    private void CancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelBtnActionPerformed
        new ArtistsArchive().setVisible(true);
        dispose();
    }//GEN-LAST:event_CancelBtnActionPerformed

    /**
     * @param args the command line arguments
     */


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField ArtisticNameTxt2;
    private javax.swing.JTextField BirthDateTxt3;
    private javax.swing.JTextField BirthPlaceTxt4;
    private javax.swing.JButton CancelBtn;
    private javax.swing.JTextField FirstNameTxt1;
    private javax.swing.JTextField LastNameTxt;
    private javax.swing.JComboBox MusicGenre;
    private javax.swing.JButton SaveBtn;
    private javax.swing.JComboBox Sex;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    // End of variables declaration//GEN-END:variables

}
